/*     */ package com.yc.web.export;
/*     */ 
/*     */ import com.yc.dao.query.QueryBO;
/*     */ import com.yc.service.export.ExportAllService;
/*     */ import java.io.File;
/*     */ import java.io.FileInputStream;
/*     */ import java.io.FileOutputStream;
/*     */ import java.io.IOException;
/*     */ import java.net.URLEncoder;
/*     */ import java.text.SimpleDateFormat;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Date;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ import javax.servlet.ServletContext;
/*     */ import javax.servlet.ServletException;
/*     */ import javax.servlet.ServletOutputStream;
/*     */ import javax.servlet.http.HttpServlet;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ import org.apache.poi.hssf.usermodel.HSSFSheet;
/*     */ import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/*     */ 
/*     */ public class ExportAllServlet extends HttpServlet
/*     */ {
/*     */   private String createDownLoadPath()
/*     */   {
/*  38 */     String path = getServletContext().getRealPath("/");
/*  39 */     path = path + "downloadtemp" + File.separator;
/*  40 */     if (!new File(path).isDirectory()) {
/*  41 */       File file = new File(path);
/*  42 */       file.mkdir();
/*     */     }
/*  44 */     return path;
/*     */   }
/*     */ 
/*     */   private void deleteTempFile(String path)
/*     */   {
/*  49 */     File file = new File(path);
/*  50 */     file.delete();
/*     */   }
/*     */ 
/*     */   public void doPost(HttpServletRequest request, HttpServletResponse response)
/*     */     throws ServletException, IOException
/*     */   {
/*  57 */     String strBtsName = new String(request.getParameter("btsName").getBytes("ISO-8859-1"), "utf-8");
/*  58 */     String strQueryTable = request.getParameter("allTable");
/*  59 */     String registBeginTime = request.getParameter("registBeginTime");
/*  60 */     String registEndTime = request.getParameter("registEndTime");
/*  61 */     String[] table = strQueryTable.split(";");
/*  62 */     int count = table.length;
/*  63 */     if ((table == null) || (count == 0)) {
/*  64 */       return;
/*     */     }
/*  66 */     List tableList = new ArrayList();
/*  67 */     for (int i = 0; i < count; i++)
/*     */     {
/*  69 */       String strID = table[i];
/*  70 */       QueryBO bo = (QueryBO)Contants.queryMap.get(strID);
/*  71 */       tableList.add(bo);
/*     */     }
/*     */ 
/*  74 */     String fileName = Contants.open.format(new Date()) + "导出结果.xls";
/*  75 */     String path = createDownLoadPath();
/*  76 */     FileOutputStream fileOut = null;
/*     */     try
/*     */     {
/*  79 */       HSSFWorkbook workbook = new HSSFWorkbook();
/*  80 */       fileOut = new FileOutputStream(path + fileName);
/*     */ 
/*  82 */       ExportAllService service = new ExportAllService();
/*  83 */       Map map = service.export(tableList, strBtsName, registBeginTime, registEndTime);
/*  84 */       Iterator key = map.keySet().iterator();
/*  85 */       while (key.hasNext())
/*     */       {
/*  87 */         QueryBO bo = (QueryBO)key.next();
/*  88 */         List list = (List)map.get(bo);
/*  89 */         if ((list != null) && (list.size() > 0))
/*     */         {
/*  91 */           HSSFSheet sheet = workbook.createSheet(bo.getName());
/*  92 */           ExportUtil.wirteToExcel(sheet, list, Integer.parseInt(bo.getId()));
/*     */         }
/*     */       }
/*  95 */       workbook.write(fileOut);
/*  96 */       fileOut.flush();
/*  97 */       fileOut.close();
/*     */     }
/*     */     catch (Exception ioe)
/*     */     {
/* 101 */       ioe.printStackTrace();
/*     */     }
/*     */ 
/*     */     try
/*     */     {
/* 106 */       File file = new File(path + fileName);
/* 107 */       ServletOutputStream servletOutputStream = response.getOutputStream();
/*     */ 
/* 109 */       fileName = URLEncoder.encode(fileName, "UTF-8");
/* 110 */       response.setHeader("Content-disposition", "attachment; filename=\"" + fileName + "\"");
/* 111 */       response.setContentLength((int)file.length());
/* 112 */       response.setContentType("application/x-download;charset=UTF-8");
/*     */ 
/* 114 */       byte[] b = new byte[1024];
/* 115 */       int i = 0;
/* 116 */       FileInputStream fis = new FileInputStream(file);
/* 117 */       while ((i = fis.read(b)) > 0)
/*     */       {
/* 119 */         servletOutputStream.write(b, 0, i);
/*     */       }
/* 121 */       servletOutputStream.flush();
/*     */ 
/* 123 */       file.delete();
/*     */     }
/*     */     catch (IOException ioe) {
/* 126 */       ioe.printStackTrace();
/*     */     }
/*     */   }
/*     */ 
/*     */   public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
/*     */   {
/* 132 */     doPost(request, response);
/*     */   }
/*     */ }

/* Location:           E:\java\apache-tomcat-6.0.36\webapps\btsm\WEB-INF\classes\
 * Qualified Name:     com.yc.web.export.ExportAllServlet
 * JD-Core Version:    0.6.2
 */